﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "Create";
    Layout = "~/views/shared/_AdminLayout.cshtml";
}

<h4>@T("Admin.Productvariant.Create.Title")</h4>
@{
    
 var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}



@Html.Partial("_ProductVariantInfo_CreateOrEdit")


   
@Styles.Render("~/content/fineuploader/fineuploader-3.5.0.css")
@Scripts.Render("~/content/fineuploader/fineuploader-3.5.0.js")
@Scripts.Render("~/content/fineuploader/iframe.xss.response-3.5.0.js")

@section scripts{

    
    <script>
        var init = @jsonStr;


        function ProductVariantModel() {
            var self = this;
            self.Id = ko.observable(init.Id);
            self.Name = ko.observable(init.Name).extend({
                required: { message: '@T("Admin.ProductVariant.NeedName")' },
                maxLength: 400
            });
            self.Price = ko.observable(init.Price).extend({
                required: { message: '@T("Admin.ProductVariant.NeedPrice")' },
                number: { message: '@T("Admin.ProductVariant.PriceFormatError")' }
            });
            self.Sku = ko.observable(init.Sku);
            self.DisplayOrder = ko.observable(init.DisplayOrder).extend({
                required: { message: '@T("Admin.ProductVariant.NeedDisplayOrder")' },
                digit: { message: '@T("Admin.ProductVariant.DisplayOrderFormatError")' }
            });
            self.StockQuantity = ko.observable(init.StockQuantity).extend({
                required: { message: '@T("Admin.ProductVariant.NeedStockQuantity")' },
                digit: { message: '@T("Admin.ProductVariant.StockQuantityFormatError")' }
            });
            self.PictureId = ko.observable(init.PictureId).extend({
                notEqual:{ message: '@T("Admin.ProductVariant.NeedPicture")',params:0}
            });
            self.PictureUrl = ko.observable(init.PicUrl);
            self.Published = ko.observable(init.Published);
            self.ProductId = ko.observable(init.ProductId);
            self.PictureId = ko.observable(0);
            
            self.SaveProductVariantInfo = function() {
                if (this.isValid()) {
                    $.ajax({
                        url: "/api/ProductVariantApi/InsertProductVariantInfo",
                        type: 'post',
                        data: ko.toJSON(self),
                        contentType: 'application/json',
                        success: function(result) {

                            if (result.Status == "0") {
                                location.href = '/admin/Product/Edit?productId=' + self.ProductId() + "&tab=pv";
                            } else {
                                toastr.error(result.Message);
                            }
                        }
                    });
                } else {
                    self.errors.showAllMessages();
                }
            };

        }

        var vm = new ProductVariantModel();
        vm.errors = ko.validation.group(vm);
        ko.applyBindings(vm);


        $(document).ready(function() {

            var errorHandler = function(event, id, fileName, reason) {
                qq.log("id: " + id + ", fileName: " + fileName + ", reason: " + reason);
            };


            var uploader2 = new qq.FineUploader({
                element: $('#manualUploadModeExample')[0],
                autoUpload: true,
                uploadButtonText: "Select Files",
                request: {
                    endpoint: "/FineUploader/UploadFile",
                    params: { "extraParam1": "true", "picSize": "100" }
                },
                callbacks: {
                    onError: errorHandler,
                    onComplete: function(id, fileName, responseJSON) {
                        if (responseJSON.success) {
                            vm.PictureId(responseJSON.OriginalPictureId);
                            vm.PictureUrl(responseJSON.NewPictureUrl);
                        }
                    }
                }
            });
        });
    </script>
}